//
//  ZLDeviceExtension.swift
//  ZLExtension
//
//  Created by 赵振磊 on 2023/1/8.
//

import UIKit

//MARK: 机型相关
public extension UIDevice {
    /// 机型
    enum ZLDeviceName: String {
        case unknow = "未知"
        case iPod5_1 = "iPod5,1"
        case iPod7_1 = "iPod7,1"
        case iPhone3_1 = "iPhone3,1"
        case iPhone3_2 = "iPhone3,2"
        case iPhone3_3 = "iPhone3,3"
        case iPhone4_1 = "iPhone4,1"
        case iPhone5_1 = "iPhone5,1"
        case iPhone5_2 = "iPhone5,2"
        case iPhone5_3 = "iPhone5,3"
        case iPhone5_4 = "iPhone5,4"
        case iPhone6_1 = "iPhone6,1"
        case iPhone6_2 = "iPhone6,2"
        case iPhone7_2 = "iPhone7,2"
        case iPhone7_1 = "iPhone7,1"
        case iPhone8_1 = "iPhone8,1"
        case iPhone8_2 = "iPhone8,2"
        case iPhone8_4 = "iPhone8,4"
        case iPhone9_1 = "iPhone9,1"
        case iPhone9_3 = "iPhone9,3"
        case iPhone9_2 = "iPhone9,2"
        case iPhone9_4 = "iPhone9,4"
        case iPhone10_1 = "iPhone10,1"
        case iPhone10_4 = "iPhone10,4"
        case iPhone10_5 = "iPhone10,5"
        case iPhone10_2 = "iPhone10,2"
        case iPhone10_3 = "iPhone10,3"
        case iPhone10_6 = "iPhone10,6"
        case iPhone11_2 = "iPhone11,2"
        case iPhone11_4 = "iPhone11,4"
        case iPhone11_6 = "iPhone11,6"
        case iPhone11_8 = "iPhone11,8"
        case iPhone12_1 = "iPhone12,1"
        case iPhone12_3 = "iPhone12,3"
        case iPhone12_5 = "iPhone12,5"
        case iPhone12_8 = "iPhone12,8"
        case iPhone13_1 = "iPhone13,1"
        case iPhone13_2 = "iPhone13,2"
        case iPhone13_3 = "iPhone13,3"
        case iPhone13_4 = "iPhone13,4"
        case iPhone14_4 = "iPhone14,4"
        case iPhone14_5 = "iPhone14,5"
        case iPhone14_2 = "iPhone14,2"
        case iPhone14_3 = "iPhone14,3"
        case iPhone14_6 = "iPhone14,6"
        case iPhone14_7 = "iPhone14,7"
        case iPhone14_8 = "iPhone14,8"
        case iPhone15_2 = "iPhone15,2"
        case iPhone15_3 = "iPhone15,3"
        case iPad2_1 = "iPad2,1"
        case iPad2_2 = "iPad2,2"
        case iPad2_3 = "iPad2,3"
        case iPad2_4 = "iPad2,4"
        case iPad3_1 = "iPad3,1"
        case iPad3_2 = "iPad3,2"
        case iPad3_3 = "iPad3,3"
        case iPad3_4 = "iPad3,4"
        case iPad3_5 = "iPad3,5"
        case iPad3_6 = "iPad3,6"
        case iPad6_11 = "iPad6,11"
        case iPad6_12 = "iPad6,12"
        case iPad7_5 = "iPad7,5"
        case iPad7_6 = "iPad7,6"
        case iPad7_11 = "iPad7,11"
        case iPad7_12 = "iPad7,12"
        case iPad11_6 = "iPad11,6"
        case iPad11_7 = "iPad11,7"
        case iPad12_1 = "iPad12,1"
        case iPad12_2 = "iPad12,2"
        case iPad4_1 = "iPad4,1"
        case iPad4_2 = "iPad4,2"
        case iPad4_3 = "iPad4,3"
        case iPad5_3 = "iPad5,3"
        case iPad5_4 = "iPad5,4"
        case iPad11_3 = "iPad11,3"
        case iPad11_4 = "iPad11,4"
        case iPad13_1 = "iPad13,1"
        case iPad13_2 = "iPad13,2"
        case iPad13_16 = "iPad13,16"
        case iPad13_17 = "iPad13,17"
        case iPad2_5 = "iPad2,5"
        case iPad2_6 = "iPad2,6"
        case iPad2_7 = "iPad2,7"
        case iPad4_4 = "iPad4,4"
        case iPad4_5 = "iPad4,5"
        case iPad4_6 = "iPad4,6"
        case iPad4_7 = "iPad4,7"
        case iPad4_8 = "iPad4,8"
        case iPad4_9 = "iPad4,9"
        case iPad5_1 = "iPad5,1"
        case iPad5_2 = "iPad5,2"
        case iPad11_1 = "iPad11,1"
        case iPad11_2 = "iPad11,2"
        case iPad14_1 = "iPad14,1"
        case iPad14_2 = "iPad14,2"
        case iPad6_7 = "iPad6,7"
        case iPad6_8 = "iPad6,8"
        case iPad6_3 = "iPad6,3"
        case iPad6_4 = "iPad6,4"
        case iPad7_1 = "iPad7,1"
        case iPad7_2 = "iPad7,2"
        case iPad7_3 = "iPad7,3"
        case iPad7_4 = "iPad7,4"
        case iPad8_1 = "iPad8,1"
        case iPad8_2 = "iPad8,2"
        case iPad8_3 = "iPad8,3"
        case iPad8_4 = "iPad8,4"
        case iPad8_5 = "iPad8,5"
        case iPad8_6 = "iPad8,6"
        case iPad8_7 = "iPad8,7"
        case iPad8_8 = "iPad8,8"
        case iPad8_9 = "iPad8,9"
        case iPad8_10 = "iPad8,10"
        case iPad8_11 = "iPad8,11"
        case iPad8_12 = "iPad8,12"
        case AppleTV5_3 = "AppleTV5,3"
        case i386 = "i386"
        case x86_64 = "x86_64"
    }
    /// 类型
    enum ZLDeviceType: String {
        case unknow = "未知"
        case iPod_Touch_5 = "iPod Touch 5"
        case iPod_Touch_6 = "iPod Touch 6"
        case iPhone_4 = "iPhone 4"
        case iPhone_4s = "iPhone 4s"
        case iPhone_5 = "iPhone 5"
        case iPhone_5c = "iPhone 5c"
        case iPhone_5s = "iPhone 5s"
        case iPhone_6 = "iPhone 6"
        case iPhone_6_Plus = "iPhone 6 Plus"
        case iPhone_6s = "iPhone 6s"
        case iPhone_6s_Plus = "iPhone 6s Plus"
        case iPhone_SE_1st_generation = "iPhone SE (1st generation)"
        case iPhone_7 = "iPhone 7"
        case iPhone_7_Plus = "iPhone 7 Plus"
        case iPhone_8 = "iPhone 8"
        case iPhone_8_Plus = "iPhone 8 Plus"
        case iPhone_X = "iPhone X"
        case iPhone_XS = "iPhone XS"
        case iPhone_XS_MAX = "iPhone XS MAX"
        case iPhone_XR = "iPhone XR"
        case iPhone_11 = "iPhone 11"
        case iPhone_11_Pro = "iPhone 11 pro"
        case iPhone_11_Pro_Max = "iPhone 11 Pro Max"
        case iPhone_SE_2nd_generation = "iPhone SE (2nd generation)"
        case iPhone_12_mini = "iPhone 12 mini"
        case iPhone_12 = "iPhone 12"
        case iPhone_12_Pro = "iPhone 12 Pro"
        case iPhone_12_Pro_Max = "iPhone 12 Pro Max"
        case iPhone_13_mini = "iPhone 13 mini"
        case iPhone_13 = "iPhone 13"
        case iPhone_13_Pro = "iPhone 13 Pro"
        case iPhone_13_Pro_Max = "iPhone 13 Pro Max"
        case iPhone_SE_3rd_generation = "iPhone SE (3rd generation)"
        case iPhone_14 = "iPhone 14"
        case iPhone_14_Plus = "iPhone 14 Plus"
        case iPhone_14_Pro = "iPhone 14 Pro"
        case iPhone_14_Pro_Max = "iPhone 14 Pro Max"
        case iPad_2 = "iPad 2"
        case iPad_3 = "iPad 3"
        case iPad_4 = "iPad 4"
        case iPad_5 = "iPad 5"
        case iPad_6 = "iPad 6"
        case iPad_7 = "iPad 7"
        case iPad_8 = "iPad 8"
        case iPad_9 = "iPad 9"
        case iPad_Air = "iPad Air"
        case iPad_Air_2 = "iPad Air 2"
        case iPad_Air_3 = "iPad Air 3"
        case iPad_Air_4 = "iPad Air 4"
        case iPad_Air_5 = "iPad Air 5"
        case iPad_Mini = "iPad Mini"
        case iPad_Mini_2 = "iPad Mini 2"
        case iPad_Mini_3 = "iPad Mini 3"
        case iPad_Mini_4 = "iPad Mini 4"
        case iPad_Mini_5 = "iPad Mini 5"
        case iPad_Mini_6 = "iPad Mini 6"
        case iPad_Pro = "iPad Pro"
        case Apple_TV = "Apple TV"
        case Simulator = "Simulator"
    }
    /// 获取具体名称
    class func zl_name() -> ZLDeviceName {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }
        let name = ZLDeviceName(rawValue: identifier) ?? .unknow
        return name
    }
    /// 获取机型
    class func zl_type() -> ZLDeviceType {
        switch self.zl_name() {
        case .iPod5_1: return .iPod_Touch_5
        case .iPod7_1: return .iPod_Touch_6
        case .iPhone3_1, .iPhone3_2, .iPhone3_3: return .iPhone_4
        case .iPhone4_1: return .iPhone_4s
        case .iPhone5_1, .iPhone5_2: return .iPhone_5
        case .iPhone5_3, .iPhone5_4: return .iPhone_5c
        case .iPhone6_1, .iPhone6_2: return .iPhone_5s
        case .iPhone7_2: return .iPhone_6
        case .iPhone7_1: return .iPhone_6_Plus
        case .iPhone8_1: return .iPhone_6s
        case .iPhone8_2: return .iPhone_6s_Plus
        case .iPhone8_4: return .iPhone_SE_1st_generation
        case .iPhone9_1, .iPhone9_3: return .iPhone_7
        case .iPhone9_2, .iPhone9_4: return .iPhone_7_Plus
        case .iPhone10_1, .iPhone10_4: return .iPhone_8
        case .iPhone10_2, .iPhone10_5: return .iPhone_8_Plus
        case .iPhone10_3, .iPhone10_6: return .iPhone_X
        case .iPhone11_2: return .iPhone_XS
        case .iPhone11_4, .iPhone11_6: return .iPhone_XS_MAX
        case .iPhone11_8: return .iPhone_XR
        case .iPhone12_1: return .iPhone_11
        case .iPhone12_3: return .iPhone_11_Pro
        case .iPhone12_5: return .iPhone_11_Pro_Max
        case .iPhone12_8: return .iPhone_SE_2nd_generation
        case .iPhone13_1: return .iPhone_12_mini
        case .iPhone13_2: return .iPhone_12
        case .iPhone13_3: return .iPhone_12_Pro
        case .iPhone13_4: return .iPhone_12_Pro_Max
        case .iPhone14_4: return .iPhone_13_mini
        case .iPhone14_5: return .iPhone_13
        case .iPhone14_2: return .iPhone_13_Pro
        case .iPhone14_3: return .iPhone_13_Pro_Max
        case .iPhone14_6: return .iPhone_SE_3rd_generation
        case .iPhone14_7: return .iPhone_14
        case .iPhone14_8: return .iPhone_14_Plus
        case .iPhone15_2: return .iPhone_14_Pro
        case .iPhone15_3: return .iPhone_14_Pro_Max
            
        case .iPad2_1, .iPad2_2, .iPad2_3, .iPad2_4: return .iPad_2
        case .iPad3_1, .iPad3_2, .iPad3_3: return .iPad_3
        case .iPad3_4, .iPad3_5, .iPad3_6: return .iPad_4
        case .iPad6_11, .iPad6_12: return .iPad_5
        case .iPad7_5, .iPad7_6: return .iPad_6
        case .iPad7_11, .iPad7_12: return .iPad_7
        case .iPad11_6, .iPad11_7: return .iPad_8
        case .iPad12_1, .iPad12_2: return .iPad_9
            
        case .iPad4_1, .iPad4_2, .iPad4_3: return .iPad_Air
        case .iPad5_3, .iPad5_4: return .iPad_Air_2
        case .iPad11_3, .iPad11_4: return .iPad_Air_3
        case .iPad13_1, .iPad13_2: return .iPad_Air_4
        case .iPad13_16, .iPad13_17: return .iPad_Air_5
            
        case .iPad2_5, .iPad2_6, .iPad2_7: return .iPad_Mini
        case .iPad4_4, .iPad4_5, .iPad4_6: return .iPad_Mini_2
        case .iPad4_7, .iPad4_8, .iPad4_9: return .iPad_Mini_3
        case .iPad5_1, .iPad5_2: return .iPad_Mini_4
        case .iPad11_1, .iPad11_2: return .iPad_Mini_5
        case .iPad14_1, .iPad14_2: return .iPad_Mini_6
                
        case .iPad6_3, .iPad6_4, .iPad6_7, .iPad6_8, .iPad7_1, .iPad7_2, .iPad7_3,
             .iPad7_4, .iPad8_1, .iPad8_2, .iPad8_3, .iPad8_4, .iPad8_5, .iPad8_6,
             .iPad8_7, .iPad8_8, .iPad8_9, .iPad8_10, .iPad8_11, .iPad8_12:
            return .iPad_Pro
            
        case .AppleTV5_3: return .Apple_TV
            
        case .i386, .x86_64: return .Simulator
        default: return .unknow
        }
    }
    /// 系统版本
    class func zl_sysVersion() -> String {
        return UIDevice.current.systemVersion
    }
    /// udid
    class func zl_UUID() -> String {
        return UIDevice.current.identifierForVendor?.uuidString ?? ""
    }
}

//MARK: 判断相关
public extension UIDevice {
    /// 是否为pad
    class func zl_isIpad() -> Bool {
        switch self.zl_type() {
        case .iPad_2, .iPad_3, .iPad_4, .iPad_5, .iPad_6, .iPad_7, .iPad_8, .iPad_9,
             .iPad_Air, .iPad_Air_2, .iPad_Air_3, .iPad_Air_4, .iPad_Air_5,
             .iPad_Mini, .iPad_Mini_2, .iPad_Mini_3, .iPad_Mini_4, .iPad_Mini_5,
             .iPad_Mini_6, .iPad_Pro:
            return true
        default: return false
        }
    }
    /// 是否为iphone
    class func zl_isIphone() -> Bool {
        switch self.zl_type() {
        case .iPod_Touch_5, .iPod_Touch_6, .iPhone_4, .iPhone_4s, .iPhone_5,
             .iPhone_5c, .iPhone_5s, .iPhone_6, .iPhone_6_Plus, .iPhone_6s,
             .iPhone_6s_Plus, .iPhone_SE_1st_generation, .iPhone_7, .iPhone_7_Plus,
             .iPhone_8, .iPhone_8_Plus, .iPhone_X, .iPhone_XS, .iPhone_XS_MAX,
             .iPhone_XR, .iPhone_11, .iPhone_11_Pro, .iPhone_11_Pro_Max,
             .iPhone_SE_2nd_generation, .iPhone_12_mini, .iPhone_12, .iPhone_12_Pro,
             .iPhone_12_Pro_Max, .iPhone_13_mini, .iPhone_13, .iPhone_13_Pro,
             .iPhone_13_Pro_Max, .iPhone_SE_3rd_generation, .iPhone_14,
             .iPhone_14_Plus, .iPhone_14_Pro, .iPhone_14_Pro_Max:
            return true
        default: return false
        }
    }
}

public extension UIDevice {
    class func zl_shake(style: UIImpactFeedbackGenerator.FeedbackStyle = .medium) {
        // 创建一个触觉反馈生成器
        let impactFeedback = UIImpactFeedbackGenerator(style: style)
        // 准备触觉反馈（可选）
        impactFeedback.prepare()
        // 触发触觉反馈
        impactFeedback.impactOccurred()
    }
}
